// test.cpp : Defines the entry point for the application.
//

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <commctrl.h>
#include "resource.h"

#include <tchar.h>
#include  <vfw.h>
#pragma comment(lib, "vfw32.lib")


#define MAX_LOADSTRING 100


// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING] = TEXT("Repeater");					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING] = TEXT("PLAYER_REPEATER");			// the main window class name
TCHAR fileName[MAX_LOADSTRING];
TCHAR buffer[MAX_LOADSTRING];
HWND playerWnd;
HWND progessWnd;
int fileLength, startPos, endPos;
enum PlayerState{
	playing,
	repeating,
	continueto_play
}PLAYER_STATE;
// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;

	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszClassName	= szWindowClass;
	wcex.hCursor = NULL;
	wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
	wcex.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SMALL));
	wcex.lpszMenuName = NULL;

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable
   int width =  ::GetSystemMetrics(SM_CXFULLSCREEN);
   hWnd = CreateWindow(szWindowClass, szTitle, WS_POPUP|WS_SYSMENU|CS_DBLCLKS,
      width/4, 0,width/2, GetSystemMetrics(SM_CYCAPTION), NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }
   //Enable the Drag and Rrop
   DragAcceptFiles(hWnd, TRUE);
   SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	RECT rcClient;
	HDROP hDrop;
	static ATOM hotkey;

	switch (message)
	{

	case WM_CREATE:
		playerWnd = MCIWndCreate(hWnd, hInst, MCIWNDF_NOTIFYPOS|MCIWNDF_NOMENU|MCIWNDF_NOERRORDLG|MCIWNDF_NOTIFYMODE|MCIWNDF_SHOWALL, NULL);
		::ShowWindow(playerWnd, SW_HIDE);
		//Create the progess bar
		::GetClientRect(hWnd, &rcClient);
		progessWnd = CreateWindowEx(0, PROGRESS_CLASS, (LPTSTR) NULL, WS_CHILD | WS_VISIBLE, 0, 0, rcClient.right, rcClient.bottom, hWnd, (HMENU) 0, hInst, NULL);
		//registery the hotkey
		hotkey = GlobalAddAtom(TEXT("ZHUBICEN_REPEATER"));
		RegisterHotKey(hWnd, hotkey, 0, VK_F1);
		RegisterHotKey(hWnd, hotkey, 0, VK_F2);
		RegisterHotKey(hWnd, hotkey, 0, VK_F3);
		RegisterHotKey(hWnd, hotkey, 0, VK_F4);
		RegisterHotKey(hWnd, hotkey, 0, VK_F5);
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		EndPaint(hWnd, &ps);
		break;
	case WM_DROPFILES :
		hDrop = (HDROP)(wParam);
		::DragQueryFile(hDrop, 0, fileName, MAX_LOADSTRING);
		MCIWndOpen(playerWnd, fileName, NULL);
		MCIWndPlay(playerWnd);
		fileLength = MCIWndGetLength(playerWnd);
		SendMessage(progessWnd, PBM_SETRANGE32, 0, fileLength);
		break;
	case MCIWNDM_NOTIFYPOS:
		if(PLAYER_STATE == repeating && (LONG)lParam >= endPos){
			MCIWndPlayFromTo(playerWnd, startPos, endPos);
			break;
		}
		if(PLAYER_STATE == continueto_play){			
			MCIWndPlayFrom(playerWnd, MCIWndGetPosition(playerWnd));
			PLAYER_STATE = playing;
		}
		SendMessage(progessWnd, PBM_SETPOS, (LONG)lParam, 0);
		break;
	case WM_DESTROY:
		UnregisterHotKey(hWnd, hotkey);
		MCIWndClose(playerWnd);
		PostQuitMessage(0);
		break;
	case WM_HOTKEY:
		if(HIWORD(lParam) == VK_F3){
			startPos = MCIWndGetPosition(playerWnd);
			_stprintf(buffer, TEXT("StartPos = %d \n"), startPos);
			OutputDebugStr(buffer);
			PLAYER_STATE = playing;
		}
		if(HIWORD(lParam) == VK_F4){
			PLAYER_STATE = repeating;
			endPos = MCIWndGetPosition(playerWnd);
			MCIWndPlayFromTo(playerWnd, startPos, endPos);
			_stprintf(buffer, TEXT("EndPos = %d \n"), endPos);
			OutputDebugStr(buffer);
		}
		if(HIWORD(lParam) == VK_F2){
			startPos = 0;
			endPos = 0;
			PLAYER_STATE = continueto_play;
		}
		if(HIWORD(lParam) == VK_F5){
			MCIWndOpen(playerWnd, fileName, NULL);
			MCIWndPlayFrom(playerWnd, 0);
			PLAYER_STATE = playing;
		}
		if(HIWORD(lParam) == VK_F1){
			SendMessage(hWnd, WM_DESTROY, 0, 0);
		}
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
